
<template>
    <div>
        <el-form :inline="true" :model="formInline" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
           <!-- 法人名称-->
            <el-form-item :label="nowLangue.judName " class="selectMB10">
                <el-input size="mini" disabled v-if="legalPersonLists.length==1" v-model="formInline.legalPersonName" ></el-input>
                <el-select size="mini"  clearable v-model="formInline.legalPersonCode" v-else :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="(legalPerson,index) in legalPersonLists" :key="index" :label="legalPerson.legalPersonCode" :value="legalPerson.legalPersonName">
                    </el-option>
                </el-select>
            </el-form-item>
           <!-- 产品线-->
            <el-form-item :label="nowLangue.productLine" class="selectMB10">
                <el-select size="mini" clearable v-model="formInline.productLineId" :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="(product,index) in productLineList" :key="index" :label="product.label" :value="product.value"></el-option>
                </el-select>
            </el-form-item>
            <!--备件编码-->
            <el-form-item :label="nowLangue.spareCode" class="selectMB10">
                <el-input size="mini" v-model="formInline.materialCode" :placeholder="nowLangue.pleaseImport+nowLangue.spareCode"></el-input>
            </el-form-item>
            <!--备件名称-->
            <el-form-item :label="nowLangue.spareName" class="selectMB10">
                <el-input size="mini" v-model="formInline.materialName" :placeholder="nowLangue.pleaseImport+nowLangue.spareName"></el-input>
            </el-form-item>
            <!-- 状态-->
            <el-form-item :label="nowLangue.status" class="selectMB10">
                <el-select size="mini" clearable v-model="formInline.status" :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="(status,index) in statusList" :key="index" :label="status.label" :value="status.value"></el-option>
                </el-select>
            </el-form-item>
            <div class="dayChose">
                <!--创建日期-->
                <el-form-item :label="nowLangue.createDate" class="selectMB10" >
                    <el-date-picker type="date" :editable="false" v-model="formInline.startCreateTime" clearable :placeholder="nowLangue.pleaseSelect" @change="selectedCreateTimeBegin"></el-date-picker>
                </el-form-item>
                <span v-text="nowLangue.to"></span>
                <el-form-item  class="selectMB10" >
                    <el-date-picker clearable :editable="false" v-model="formInline.endCreateTime"  type="date" :placeholder="nowLangue.pleaseSelect" @change="selectedCreateTimeEnd"></el-date-picker>
                </el-form-item>
            </div>
            <el-form-item class="selectMB10">
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="queryClick()" v-text="nowLangue.query"></el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <!--新增备件列表-->
        <label class="default-label" v-text="nowLangue.listOfNewSpareParts"></label>
        <el-table :data="productData" border style="width: 100%;" v-loading.body="loading" :element-loading-text="nowLangue.loadDesperately">
            <el-table-column type="index" header-align="center" align="center" :label="nowLangue.sequence" width="40"></el-table-column>
            <el-table-column :label="nowLangue.operate" header-align="center" align="center" >
                <template scope="scope">
                    <!--新增-->
                    <div class="operationItem bolder" v-if="!scope.row.formId" @click="add(scope.$index,scope.row)" v-text="nowLangue.addNew"></div>
                    <div class="operationItem bolder disabled" v-else v-text="nowLangue.addNew"></div>
                    <!--修改-->
                    <div class="operationItem bolder" v-if="scope.row.formId && scope.row.status==1" @click="update(scope.$index,scope.row)" v-text="nowLangue.update"></div>
                    <div class="operationItem bolder disabled" v-else v-text="nowLangue.update"></div>
                    <!--提交-->
                    <div class="operationItem bolder" v-if="scope.row.formId && scope.row.status==1" @click="submit(scope.$index,scope.row)" v-text="nowLangue.submitConfirm"></div>
                    <div class="operationItem bolder disabled" v-else v-text="nowLangue.submitConfirm"></div>
                    <!--查看-->
                    <div class="operationItem bolder" v-if="scope.row.formId" @click="see(scope.$index,scope.row)" v-text="nowLangue.see"></div>
                    <div class="operationItem bolder disabled" v-else v-text="nowLangue.see"></div>
                </template>
            </el-table-column>
            <!--产品线-->
            <el-table-column prop="productLineName" :label="nowLangue.productLine" header-align="center" show-overflow-tooltip></el-table-column>
            <!--备件编码-->
            <el-table-column prop="materialCode" :label="nowLangue.spareCode" header-align="center" show-overflow-tooltip></el-table-column>
            <!--备件名称-->
            <el-table-column prop="materialName" :label="nowLangue.spareName" header-align="center"  show-overflow-tooltip></el-table-column>
            <!--计量单位-->
            <el-table-column prop="unit" :label="nowLangue.unit" header-align="center" show-overflow-tooltip></el-table-column>
            <!--状态-->
            <el-table-column prop="statusName" :label="nowLangue.status" header-align="center" align="center" show-overflow-tooltip></el-table-column>
        </el-table>
        <el-pagination v-if="isPaging"  @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage3" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                nowLangue:{},
                languageScoped:{
                    zh:{
                        "pleaseSelect":"请选择",
                        "pleaseImport":"请输入",
                        "judName":"法人信息",
                        "productLine":"产品线" ,
                        "spareCode":"备件编码",
                        "spareName":"备件名称",
                        "status":"状态",
                        "createDate":"创建日期",
                        "to":"至",
                        "query":"查询",
                        "loadDesperately":"拼命加载中",
                        "sequence":"序",
                        "operate":"操作",
                        "addNew":"新增",
                        "submitConfirm":"提交",
                        "unit":"计量单位",
                        "listOfNewSpareParts":"新增备件列表",
                        "update":"修改",
                        "see":"查看"

                    },
                    en:{
                        "pleaseSelect":"please select",
                        "pleaseImport":"please import ",
                        "judName":"jud Name",
                        "productLine":"product Line" ,
                        "spareCode":"spare Code",
                        "spareName":"spare Name",
                        "status":"status",
                        "createDate":"create Date",
                        "to":"to",
                        "query":"query",
                        "loadDesperately":"load Desperately",
                        "sequence":"sequence",
                        "operate":"operate",
                        "addNew":"addNew",
                        "submitConfirm":"submitConfirm",
                        "unit":"unit",
                        "listOfNewSpareParts":"list of new spare parts",
                        "update":"update",
                        "see":"see"
                    }
                },
                // 查询表单
                formInline:{
                    legalPersonId:'',
                    legalPersonCode:"",
                    legalPersonName:'',
                    productLineId:'',//产品线ID
                    status:"",//状态
                    startCreateTime:"",//创建日期开始
                    endCreateTime:"",//创建日期截止
                    materialCode:"",//备件编码
                    materialName:""//备件名称
                },
                legalPersonLists:[],//法人名称列表
                legalPersonDropDown:[], //法人名称
                productLineList:[],//产品线集合
                statusList:[],//状态集合
                productData:[],//列表

                //按钮禁用
                selectDisabled:false,
                submitDisabled:false,

                isClickBySelect:false,//标记查询按钮是否点击过
                whichQueryFlag:false,//判断刚进入页面时调取哪个查询接口   从哪个接口进入的新增等页面就返回调取那个接口
                // 分页
                loading:false,
                isPaging:true,    //是否显示分页
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                currentPage:1,    //当前显示3页
                currentPage3:3,
                totalNumber: 1,     //总条数
                totalPage:1    //总页数
            };
        },
        created:function(){
            this.languageType=="zh"?this.nowLangue=this.languageScoped.zh:this.nowLangue=this.languageScoped.en;
        },
        computed:{
            languageType(){
                return this.$store.state.languageType;
            }
        },
        watch:{
            languageType(){
                this.languageType=="zh"?this.nowLangue=this.languageScoped.zh:this.nowLangue=this.languageScoped.en;
            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'EngineeringReplacement');
            //接收新增、修改、查看返回的参数，判断调取哪个查询接口
            this.whichQueryFlag=this.$route.query.whichQueryFlag;
            if(this.whichQueryFlag){
                this.isClickBySelect=true;
            }else{
                this.isClickBySelect=false;
            }
            _this.legalPersonDropDownValue();
            this.getCommonList();
            this.queryLine();
        },
        methods:{
            // 选择创建开始日期
            selectedCreateTimeBegin(val){
                var _this=this;
                _this.formInline.startCreateTime = val;
            },
            // 选择创建结束日期
            selectedCreateTimeEnd(val){
                var _this=this;
                _this.formInline.endCreateTime = val;
            },
            // 查询法人名称
            legalPersonDropDownValue(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/ofCurrentUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonLists = data.data;
                            if(_this.legalPersonLists.length == 1){
                                _this.formInline.legalPersonId =_this.legalPersonLists[0].id;
                                _this.formInline.legalPersonCode =_this.legalPersonLists[0].legalPersonCode;
                                _this.formInline.legalPersonName =_this.legalPersonLists[0].legalPersonName;
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //下拉框公共接口
            getCommonList(){
                var _this=this;
                var tmp=["substituteForm.productLine","substituteForm.status"];
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"metadata/getPullDownList",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.productLineList=data.data['substituteForm.productLine'];//产品线集合
                            _this.statusList=data.data['substituteForm.status'];//状态集合
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //查询按钮点击
            queryClick(){
                this.pageIndex=1;
                this.pageSize=10;
                this.isClickBySelect=true;
                this.whichQueryFlag=true;
                this.queryLine();
            },
            //查询列表
            queryLine(){
                var _this=this;
                _this.selectDisabled=true;
                 _this.loading=true;
                 var urlAddress="";
                 if(this.isClickBySelect){
                     urlAddress="projectSubstitute/queryMaterialByPage";
                 }else{
                     urlAddress="projectSubstitute/queryIncreasementMaterialByPage";
                 }
                 var tmp={
                     productLineId:_this.formInline.productLineId,
                     legalPersonCode:_this.formInline.legalPersonCode,
                     startCreateTime:_this.formInline.startCreateTime,
                     endCreateTime:_this.formInline.endCreateTime,
                     materialCode:_this.formInline.materialCode,
                     materialName:_this.formInline.materialName,
                     status:_this.formInline.status,
                     pageIndex:_this.pageIndex,
                     pageSize:_this.pageSize
                 };
                 $.ajax({
                     type: 'POST',
                     url: _this.metadata_service+urlAddress,
                     cache: false,
                     data:JSON.stringify(tmp),
                     dataType:'JSON',
                     contentType: "application/json;charset=utf-8",
                     success: function (data) {
                         _this.selectDisabled=false;
                         _this.loading=false;
                         if (data.success) {
                             _this.isPaging=true;
                             _this.totalPage=data.data.totalPage;
                             _this.totalNumber=data.data.totalNumber;
                             _this.productData=data.data.dataList;
                         }else{
                            _this.common.errorTip(data.errorMessage)
                         }
                     },
                     error:function(){
                         _this.selectDisabled=false;
                         _this.loading=false;
                     }
                 });
            },
            //新增备件
            add(index,row){
                this.$router.push({ path: 'CreateEReplacement', query: { productLineArray: row ,whichQueryFlag:this.whichQueryFlag}})
            },
            see(index,row){
                this.$router.push({ path: 'SeeEReplacement', query: { formId: row.formId ,whichQueryFlag:this.whichQueryFlag}})
            },
            update(index,row){
                this.$router.push({ path: 'UpdateEReplacement', query: { formId: row.formId ,whichQueryFlag:this.whichQueryFlag}})
            },
            //提交
            submit(index,row){
                var _this=this;
                _this.submitDisabled=true;
                $.ajax({
                    type: 'GET',
                    url: _this.metadata_service+"projectSubstitute/sumbit?formId="+row.formId,
                    cache: false,
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.submitDisabled=false;
                            var successMessage=_this.languageType=="zh"?'提交成功！':'Submit Success';
                            _this.common.successTip(successMessage);
                            _this.queryLine();
                        }else{
                            _this.submitDisabled=false;
                            _this.common.errorTip(data.errorMessage);
                        }
                    },
                    error:function(){
                        _this.submitDisabled=false;
                    }
                });
            },
            //分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.queryLine();
            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.pageIndex=val;
                this.queryLine();
            },
        }
    }
</script>



